#!/usr/bin/python3
import openpyxl
import json
import os


class ReadExcel:
    #path:文件路径
    #sheetName 页签名称
    #col:从**列开始 row:从**行开始 maxCol:到**列结束 title key:主键 可以为空
    def SelectField(path,sheetName,row,col,maxcol,key,title):
        # 'excel/eee.xlsx'
        book = openpyxl.load_workbook(path)
        sheet = book [sheetName]
        rows = sheet.max_row
        print(rows)
        cols = sheet.max_column
        if maxcol>0: 
            cols = maxcol
        i = 3
        c = 1

        if row>0:
            i = row
        if col >0:
            c = col

        typeStr = key
        jsonData = "{"
        if typeStr == '':
            jsonData += '\n\t' +'"'+ title + '"' + ':['
        else:
            jsonData += '\n\t' + '"'+ title + '"'+ ':{'
        while i<= rows:
            for j in range(c,cols+1):
                #print(c,"  : ",j)
                end = ','
                if j==c: #句头
                    for k in range(1,cols+1):
                        #根据第一行的字段匹配
                        if sheet.cell(1,k).value == typeStr:
                                jsonData += '\n\t\t' + '"' + str(sheet.cell(i,k).value) + '":' + '{'

                #类型判断
                value = sheet.cell(i,j).value;
                if value == None:
                    #第二行为字段名
                    if str(sheet.cell(3,j).value) == 'string':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value +'":' + '""'
                    if str(sheet.cell(3,j).value) == 'int':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value +'":' + 'null'
                    if str(sheet.cell(3,j).value) == 'float':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value +'":' + 'null'
                    if str(sheet.cell(3,j).value) == 'array' or str(sheet.cell(3,j).value) == 'array2'or str(sheet.cell(3,j).value) == 'arrayS':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value +'":' + 'null'

                else:
                    if str(sheet.cell(3,j).value) == 'boolean':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value + '":'  + str(bool(sheet.cell(i,j).value)).lower()
                    if str(sheet.cell(3,j).value) == 'int':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value + '":' + str(sheet.cell(i,j).value)
                    if str(sheet.cell(3,j).value) == 'float':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value + '":' + str(sheet.cell(i,j).value)
                    if str(sheet.cell(3,j).value) == 'string':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value + '":' + '"' +str(sheet.cell(i,j).value) + '"'
                    if str(sheet.cell(3,j).value) == 'array':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value + '":'+ '[' + str(sheet.cell(i,j).value) + ']';
                    if str(sheet.cell(3,j).value) == 'array2':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value + '":'+ '[[' + str(sheet.cell(i,j).value).replace(';','],[') + ']]';
                    if str(sheet.cell(3,j).value) == 'arrayS':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value + '":'+ '[' + '"' + str(sheet.cell(i,j).value).replace(',','","') + '"' + ']';

                if(sheet.cell(2,j).value == 'block_order_mark'):
                    print('block_order_mark',value)
                #print(sheet.cell(1,j).value ,":", sheet.cell(i,j).value)
                if j == cols: #句尾
                    end = ''
                    if i < rows:
                        jsonData += '\n\t\t' + '},'
                    else:
                        jsonData += '\n\t\t' + '}'
             
                    if i == rows:
                        if typeStr == '':
                            jsonData += '\n\t' + ']'
                        else:
                            jsonData += '\n\t' + '}'

                        jsonData += '\n' + '}'
                    #print(jsonData)
                jsonData += end
            i=i+1
        print(jsonData)
        if not os.path.exists('json'):
            os.makedirs('json')
        file = open("json/" + sheetName + '.json','w')
        file.write(jsonData)
        file.close()

    #key2 第二级key使用的表头名称
    def SelectField2(path,sheetName,row,col,maxcol,key,key2,title):
        book = openpyxl.load_workbook(path)
        sheet = book [sheetName]
        rows = sheet.max_row
        print(rows)
        cols = sheet.max_column
        if maxcol>0: 
            cols = maxcol
        i = 3
        c = 1

        if row>0:
            i = row
        if col >0:
            c = col

        typeStr = key
        typeStr2 = key2

        head1 = None
        head2 = None
        
        jsonData = "{"
        if typeStr == '':
            jsonData += '\n\t' +'"'+ title + '"' + ':['
        else:
            jsonData += '\n\t' + '"'+ title + '"'+ ':{'
        while i<= rows:
            for j in range(c,cols+1):
                #print(c,"  : ",j)
                end = ','
                if j==c: #句头
                    for k in range(1,cols+1):
                        #根据第一行的字段匹配
                        if sheet.cell(1,k).value == typeStr:
                            head1 = k
                        if sheet.cell(1,k).value == typeStr2:
                            head2 = k

                    if sheet.cell(i,head1).value != None:
                        jsonData += '\n\t\t' + '"' + str(sheet.cell(i,head1).value) + '":' + '{'
                    #if sheet.cell(i,head1).value == None:
                    jsonData += '\n\t\t\t' + '"' + str(sheet.cell(i,head2).value) + '":' + '{'

                #类型判断
                value = sheet.cell(i,j).value;
                if value == None:
                    #第二行为字段名
                    if str(sheet.cell(3,j).value) == 'string':
                        jsonData += '\n\t\t\t"' + sheet.cell(2,j).value +'":' + '""'
                    if str(sheet.cell(3,j).value) == 'int':
                        jsonData += '\n\t\t\t"' + sheet.cell(2,j).value +'":' + 'null'
                    if str(sheet.cell(3,j).value) == 'array' or str(sheet.cell(3,j).value) == 'array2': 
                        jsonData += '\n\t\t\t"' + sheet.cell(2,j).value +'":' + 'null'
                    if str(sheet.cell(3,j).value) == 'float':
                        jsonData += '\n\t\t\t"' + sheet.cell(2,j).value +'":' + 'null'
                else:
                    if str(sheet.cell(3,j).value) == 'boolean':
                        jsonData += '\n\t\t\t"' + sheet.cell(2,j).value + '":'  + str(bool(sheet.cell(i,j).value)).lower()
                    if str(sheet.cell(3,j).value) == 'int':
                        jsonData += '\n\t\t\t"' + sheet.cell(2,j).value + '":' + str(sheet.cell(i,j).value)
                    if str(sheet.cell(3,j).value) == 'float':
                        jsonData += '\n\t\t\t"' + sheet.cell(2,j).value + '":' + str(sheet.cell(i,j).value)
                    if str(sheet.cell(3,j).value) == 'string':
                        jsonData += '\n\t\t\t"' + sheet.cell(2,j).value + '":' + '"' +str(sheet.cell(i,j).value) + '"'
                    if str(sheet.cell(3,j).value) == 'array':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value + '":'+ '[' + str(sheet.cell(i,j).value) + ']';
                    if str(sheet.cell(3,j).value) == 'array2':
                        jsonData += '\n\t\t"' + sheet.cell(2,j).value + '":'+ '[[' + str(sheet.cell(i,j).value).replace(';','],[') + ']]';

           
                #print(sheet.cell(1,j).value ,":", sheet.cell(i,j).value)
                if j == cols: #句尾
                    end = ''
                    if i < rows:
                        if sheet.cell(i+1,head1).value != None:
                            jsonData += '\n\t\t\t' + '}'
                            jsonData += '\n\t\t\t' + '},'
                        else:
                             jsonData += '\n\t\t\t' + '},'   
                        #jsonData += '\n\t\t\t' + '},'
                    else:
                        jsonData += '\n\t\t\t' + '}'
             
                    if i == rows:
                        if typeStr == '':
                            jsonData += '\n\t\t' + ']'
                        else:
                            jsonData += '\n\t\t' + '}'
                            jsonData += '\n\t\t' + '}'

                        jsonData += '\n' + '}'
                    #print(jsonData)
                jsonData += end
            i=i+1
        print(jsonData)
        if not os.path.exists('json'):
            os.makedirs('json')
        file = open("json/" + sheetName + '.json','w')
        file.write(jsonData)
        file.close()

ReadExcel.SelectField('excel/table.xlsx','monster_Refresh_config',4,1,0,'阶段id','monster_Refresh_config')
ReadExcel.SelectField('excel/table.xlsx','secondGun_unlock_config',4,1,0,'位置id','secondGun_unlock_config')
ReadExcel.SelectField('excel/table.xlsx','monster_base_config',4,1,0,'怪物id','monster_base_config')
ReadExcel.SelectField('excel/table.xlsx','moveLine_config',4,1,0,'路线id','moveLine_config')
ReadExcel.SelectField('excel/table.xlsx','current_config',4,1,0,'字段名','current_config')
ReadExcel.SelectField('excel/table.xlsx','bullet_config',4,1,0,'子弹id','bullet_config')
ReadExcel.SelectField('excel/table.xlsx','secondGun_specialSkill_config',4,1,0,'技能id','secondGun_specialSkill_config')
ReadExcel.SelectField('excel/table.xlsx','secondaryGun_config',4,1,0,'副炮等级','secondaryGun_config')
ReadExcel.SelectField('excel/table.xlsx','tasteMainGun_config',4,1,0,'体验id','tasteMainGun_config')
ReadExcel.SelectField('excel/table.xlsx','skin_config',4,1,0,'皮肤id','skin_config')
ReadExcel.SelectField2('excel/table.xlsx','MainGun__config',4,1,0,'主炮id','属性等级','MainGun__config')
ReadExcel.SelectField('excel/table.xlsx','signIn_config',4,1,0,'签到id','signIn_config')
ReadExcel.SelectField('excel/table.xlsx','skin_config',4,1,0,'皮肤id','skin_config');
ReadExcel.SelectField('excel/table.xlsx','level_monster_Refresh_config',4,1,0,'阶段id','level_monster_Refresh_config');
ReadExcel.SelectField('excel/table.xlsx','turntable_config',4,1,0,'转盘ID','turntable_config');
ReadExcel.SelectField('excel/table.xlsx','prop_config',4,1,0,'道具id','prop_config');
ReadExcel.SelectField('excel/table.xlsx','secretShop_config',4,1,0,'商店ID','secretShop_config');
ReadExcel.SelectField('excel/table.xlsx','gunRandomSpend_config',4,1,0,'概率等级id','gunRandomSpend_config');
ReadExcel.SelectField('excel/table.xlsx','gunRandomLevel_config',4,1,0,'概率id','gunRandomLevel_config');
ReadExcel.SelectField('excel/table.xlsx','buildingPlatform_config',4,1,0,'等级id','buildingPlatform_config');
ReadExcel.SelectField('excel/table.xlsx','gunResolve_config',4,1,0,'分解id','gunResolve_config');







